<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;

class MenuController extends BaseController {
  /**
   *  @brief  : 后台-左侧菜单
   *  @return : page
   *  @details: 
   */
  public function indexAction() {

    $RoleID = table('Ky_Admin', 'rydb.websms')->where(['AdminId' => $this->auid])->value('RoleID');
    $NodeID = table('Ky_AdminRoleAction', 'rydb.websms')->where(['State' => 1, 'RoleID' => $RoleID])->column('NodeID');
    $NodeID = array_keys($NodeID);
    
    // 子菜单
    $list1 = table('Ky_AdminAction', 'rydb.websms')
            ->where(['IsDelete' => 0, 'ParentId' => ['EGT', 0]])
            ->whereIn('NavId', $NodeID)
            ->order(['ParentId', 'SortID', 'NavTitle'])
            ->select();

    // 父节点
    $ParentId = table('Ky_AdminAction', 'rydb.websms')
            ->where(['IsDelete' => 0, 'ParentId' => ['EGT', 0], 'NavId' => ['IN', $NodeID]])
            ->group('ParentId')
            ->column('ParentId');
    $ParentId = array_keys($ParentId);

    $list2 = table('Ky_AdminAction', 'rydb.websms')
            ->where(['IsDelete' => 0, 'ParentId' => ['EGT', 0]])
            ->whereIn('NavId', $ParentId)
            ->order(['ParentId', 'SortID', 'NavTitle'])
            ->select();

    $list3 = table('Ky_AdminAction', 'rydb.websms')->where('ParentId', 0)->select();

    $list = array_merge($list3, $list2, $list1);

    $tree = list_to_tree($list, 'NavId', 'ParentId');
    return $this->fetch('', ['tree' => $tree]);

  }
}
